//
//  Stack.m
//  WMLParser
//
//  Created by 张 霄鹤 on 09-11-13.
//  Copyright 2009 xhzhang. All rights reserved.
//

#import "Stack.h"


@implementation Stack

- (id)init{
	if(self = [super init]){
		level = [[NSMutableArray alloc] init];
		cc = 0;
	}
	return self;
}

- (void)push:(id)obj{
	@synchronized(self){
		[level addObject:obj];
		cc++;
	}
}

- (BOOL)isEmpty{
	return cc == 0;
}

- (NSInteger)size{
	return cc;
}

- (id)peek{
	id ret = nil;
	if(cc > 0){
		ret = [level objectAtIndex:(cc - 1)];
	}
	return ret;
}

- (id)pop{
	id ret = nil;
	if(cc > 0){
		@synchronized(self){
			ret = [level objectAtIndex:(cc - 1)];
			[level removeLastObject];
			cc--;
		}
	}
	return ret;
}

- (void)removeAll{
	@synchronized(self){
		[level removeAllObjects];
		cc = 0;
	}
}

- (void)dealloc{
	[level removeAllObjects];
	[level release];
	[super dealloc];
}

@end
